<script setup lang="ts">
import { ref } from 'vue';
import { rulesMobile, rulesPassword, rulesCode } from '@/utils/rules'
import { showFailToast, showSuccessToast } from 'vant';
import { LoginMsMCode, LoginPassword } from '@/apis/user';
import { useUserStore } from '@/stores';
import router from '@/router'

defineOptions({
  name: 'LoginPage'
})
//定义响应式数据
const password = ref('')
const mobile = ref('')
const isAgree = ref(false)//勾选
const isPassword = ref(true)//密码登录
const code = ref('')//保存验证码
import { useMobileCode } from '@/composable/useMobileCode'
const { onSend, time, formTel } = useMobileCode(mobile)
//提交按钮
const onSubmit = async (val: object) => {
  console.log(val);
  //没勾选提示
  if (!isAgree.value) return showFailToast('请勾选我同意')
  //验证通过发送请求
  const res = isPassword.value ? await LoginPassword(val) : await LoginMsMCode(mobile.value, code.value)
  //储存用户信息
  const store = useUserStore()//定义储存的数据
  store.setStore(res.data)
  //提示用户
  showSuccessToast('登录成功')
  //跳转页面
  router.push('/user')
}

//切换表单重置
const onReset = () => {
  isPassword.value = !isPassword.value
  formTel.value?.resetValidation()
}

const isShow = ref(false)

</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册" @click="router.push('/register')"></cp-nav-bar>
    <!-- 头部 -->
    <div class="login-head">
      <h3>{{ isPassword ? '密码' : '短信验证码' }}登录</h3>
      <a href="javascript:;">
        <span @click="onReset">{{ isPassword ? '短信验证码' : '密码' }}登录</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- 表单 -->
    <van-form autocomplete="off" @submit="onSubmit" ref="formTel">
      <van-field placeholder="请输入手机号" type="tel" name="mobile" v-model="mobile" :rules="rulesMobile"></van-field>
      <van-field placeholder="请输入密码" :type="`${isShow ? 'text' : 'password'}`" name="password" v-model="password"
        v-if="isPassword" :rules="rulesPassword">
        <template #button>
          <cp-icon class="icon-img" @click="isShow = !isShow"
            :name="`${isShow ? 'login-eye-on' : 'login-eye-off'}`"></cp-icon>
        </template></van-field>
      <van-field v-model="code" name="code" placeholder="请输入短信验证码" :rules="rulesCode" v-else>
        <template #button>
          <span class="btn-send" @click="onSend" :class="{ active: time > 0 }">{{ time > 0 ? `${time}秒后发送验证码` : "发送验证码"
            }}</span>
        </template>
      </van-field>
      <div class="cp-cell">
        <van-checkbox v-model="isAgree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>
    <!-- 底部 -->
    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <a class="icon"
        href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&client_id=102015968&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fconsult-patients.itheima.net%2Flogin%2Fcallback">
        <img src="@/assets/qq.svg" alt="" />
      </a>
    </div>
  </div>
</template>

<style scoped lang="scss">
@import '@/styles/login.scss';
</style>